import 'package:flutter/material.dart';
import 'package:flutter_app/redux/state.dart';
import 'package:flutter_app/model/user.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_app/views/Credit/product_select.dart';
import 'package:flutter_app/widgets/product_detail.dart';
import 'package:flutter_app/widgets/Button/index.dart';

class Credit extends StatefulWidget {
  Credit({Key key}) : super(key: key);

  _CreditState createState() => _CreditState();
}

class _CreditState extends State<Credit> {
  Map _selectProduct;

  @override
  Widget build(BuildContext context) {
    ///通过 StoreConnector 关联 APPState 中的 User
    return new StoreConnector<APPState, User>(
      ///通过 converter 将 APPState 中的 userInfo返回
      converter: (store) => store.state.userInfo,

      ///在 userInfo 中返回实际渲染的控件
      builder: (context, userInfo) {
        return Scaffold(
          backgroundColor: Colors.grey[100],
          appBar: AppBar(
            centerTitle: false,
            // elevation: 0,
            title: Text(
              userInfo.mobileNumber,
            ),
            // leading: Text('Atome Credit'),
          ),
          body: ListView(
            padding: EdgeInsets.all(16),
            children: <Widget>[_renderLoanStart()],
          ),
        );
      },
    );
  }

  Widget _renderLoanStart() {
    return Card(
      elevation: 0.5,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(12.0))),
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            ProductSelect(
                onChange: (Map product) => setState(() {
                      _selectProduct = product;
                    })),
            Padding(
              child: ProductDetail(product: _selectProduct),
              padding: EdgeInsets.only(top: 10, bottom: 10),
            ),
            Container(
              child: Button(
                onClick: _selectProduct != null && _selectProduct.isNotEmpty
                    ? loanRequest
                    : null,
                text: 'Apply',
              ),
            )
          ],
        ),
      ),
    );
  }

  void loanRequest() {
    print('loanRequest ${_selectProduct['productId']}');
  }
}
